﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

//xhk 6.14
//血条管理
public class HitPoint : MonoBehaviour
{
    Image blood;
    float maxHP;
    float currentHP;
    static Color start = new Color(100,0,0,1);
    static Color end = Color.green;

    private void Start()
    {
        blood = GameObject.Instantiate(Resources.Load("Prefabs/Blood") as GameObject, transform.position + new Vector3(0, (ConstOfMap.yGap) / 2, 0), Quaternion.identity, transform).GetComponentInChildren<Image>();
    }

    private void Update()
    {
        if(currentHP<=0)
        {
            MapManager.Instance.CleanBuilding(gameObject);
        }
        blood.fillAmount = (float)currentHP / (float)maxHP;
        blood.color= Color.Lerp(start, end, blood.fillAmount);
    }

    public void attacked(float attack)
    {
        currentHP = currentHP - attack;
        if(currentHP<0)
        {
            currentHP = 0;
        }
    }

    public void setHP(float hp)
    {
        currentHP = hp;
    }

    public void setMaxHP(float hp)
    {
        maxHP = hp;
    }

    public void FullHp()
    {
        setHP(getMaxHP());
    }

    public float getCurrentHP()
    {
        return currentHP;
    }
    public float getMaxHP()
    {
        return maxHP;
    }

    public void changeMaxHP(float v)
    {
        if(v>0)
        {
            maxHP += v;
            currentHP += v;
        }
        else
        {
            maxHP += v;
            if(currentHP>maxHP)
            {
                currentHP = maxHP;
            }
        }
    }

    public void changeHP(float v)
    {
        currentHP = currentHP + v;
        if (currentHP < 0)
        {
            currentHP = 0;
        }
        if (currentHP > maxHP)
        {
            currentHP = maxHP;
        }
    }

    public void healHP(float h)
    {
        currentHP += maxHP * h;
        if (currentHP > maxHP)
            currentHP = maxHP;
    }
}
